package com.example.weatherview.analyze

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.File

/**
 * @author Tom.wu
 * @Description:
 * @Package: com.example.weatherview.analyze
 * @data  2024/3/14 16:16
 */
object FileAnalyzer {


    private var mListener:AnalyzerListener?=null

    private val mFiles = arrayListOf<File>()

    suspend fun start(file: File){
        withContext(Dispatchers.IO){
            analyze(file)
        }
        mListener?.onFinish(mFiles)
    }


    private fun analyze(directory: File){
        val files = directory.listFiles()
        if (files != null) {
            for (file in files) {
                if (file.isDirectory) {
                    // 递归调用以处理子目录
                    analyze(file)
                } else {
                    mFiles.add(file)
                    mListener?.onAnalyzer(file)
                }
            }
        }
    }


    fun setListener(listener: AnalyzerListener){
        mListener = listener
    }

    interface AnalyzerListener{

        fun onAnalyzer(file:File)

        fun onFinish(files: List<File>)
    }

}